package homelesspartners.client;

import homelesspartners.client.model.City;
import homelesspartners.client.model.Gift;
import homelesspartners.client.model.Shelter;
import homelesspartners.client.model.User;
import homelesspartners.client.widgets.CitiesWidget;
import homelesspartners.client.widgets.GiftTabWidget;
import homelesspartners.client.widgets.HomeTabWidget;
import homelesspartners.client.widgets.LoginWidget;
import homelesspartners.client.widgets.SheltersWidget;
import homelesspartners.client.widgets.ShoppingListWidget;
import homelesspartners.client.widgets.StoriesWidget;
import homelesspartners.client.widgets.VolunteerTabWidget;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;

public class Controller {

	public static void updateCitiesList(ArrayList cityList) {
		City.setCities(cityList);
		CitiesWidget.getInstance().refresh();
		HomeTabWidget.getInstance().refresh();
	}

	public static void doStartButtonClicked() {

		if (!CitiesWidget.getInstance().isPopulated()) {
			ServerFacade.loadCities();
		}
		GiftTabWidget.getInstance().showChooseCity();
	}

	public static void doCityWidgetClicked() {
		City city = User.getInstance().getCurrentCity();
		if (!city.isPopulated()) {
			ServerFacade.loadShelters(city);
		}
		GiftTabWidget.getInstance().showChooseShelter();
	}

	public static void updateSheltersList(City aCity, ArrayList shelters) {
		aCity.setShelters(shelters);
		SheltersWidget.getInstance().refresh(aCity);
		VolunteerTabWidget.getInstance().noticeCityUpdated(aCity);
	}

	public static void completeLoadPageOfStories(Shelter shelter, Object result) {
		if (result == null) {
			failedLoadPageOfStories(null);
			return;
		}
		shelter.setPageOfStories((ArrayList) result);
		StoriesWidget.getInstance().refresh(shelter);
	}

	public static void failedLoadPageOfStories(Throwable ex) {
		Window.alert("Could not load the next stories.");
		if (ex != null) {
			Window.alert(ex.getMessage());
		}
	}

	public static void pledgeGift(Gift gift) {
		if (ShoppingListWidget.getInstance().isFull()) {
			Window
					.alert("We're sorry, but you have reached the limit of the number of gifts you can pledge via the website.  If you would like to give more, please contact us directly to make arrangements.  Thank you so much for your generousity!");
		} else {
			ServerFacade.pledgeGift(gift);
		}
	}

	public static void updateLogin(User aUser) {
		LoginWidget.getInstance().hide();
		User.getInstance().updateWith(aUser);
		Window.alert("Thanks!  You are now logged in and can continue working.");
		ServerFacade.loadShoppingList();
		VolunteerTabWidget.getInstance().noticeLogin();
	}

	public static void unableToLogin() {
		Window.alert("Could not login, invalid username or password.\n\nNOTE: If you have not logged in before, please click 'New User' to create an account.");
	}

	public static void unableToCreateNewUser() {
		Window.alert("Could not create an account");
	}

	public static void unableToLoadShoppingList() {
		Window.alert("Could not load the shopping list");
	}

	public static void removeFromShoppingList(Gift gift) {
		ShoppingListWidget.getInstance().removeGift(gift);
	}

}
